﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section12
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "EVENT FOR CONTROL"

	Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Translate()
        If Not IsPostBack Then


            Me.EnableViewState = False
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidMSDSID.Value = Request.QueryString("Id")
                btnPrevious.Attributes.Add("onclick", "location.href='Section11.aspx?Id=" & hidMSDSID.Value & "'")
            End If
            CheckData()
        End If
	End Sub

	Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
		Using data As New VSEntities
			Try
				Dim p As MSDSSection12
				p = (From q In data.MSDSSection12 Where q.Section12Id = hidID.Value).FirstOrDefault()
				Dim _isNew = True
				If Not p Is Nothing Then
					_isNew = False
				Else
					p = New MSDSSection12
				End If
                With p
					'.S12110 = txtS12110.Text.Trim
					'.S12111 = txtS12111.Text.Trim
					'.S12112 = txtS12112.Text.Trim
					'.S12113 = txtS12113.Text.Trim

					'.S12120 = txtS12120.Text.Trim
					'.S12121 = txtS12121.Text.Trim
					'.S12122 = txtS12122.Text.Trim
					'.S12123 = txtS12123.Text.Trim

					'.S12130 = txtS12130.Text.Trim
					'.S12131 = txtS12131.Text.Trim
					'.S12132 = txtS12132.Text.Trim
					'.S12133 = txtS12133.Text.Trim

					'.S12140 = txtS12140.Text.Trim
					'.S12141 = txtS12141.Text.Trim
					'.S12142 = txtS12142.Text.Trim
					'.S12143 = txtS12143.Text.Trim

                    .S1221 = txtS1212.Text.Trim
                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
				If _isNew Then
					p.Created = Now
					p.Creator = Session("UserName")
					data.MSDSSection12.AddObject(p)
				Else
					p.Modified = Now
					p.Modifier = Session("UserName")
				End If
				data.SaveChanges()

				'' Xử lý phàn Detail
				hidID.Value = p.Section12Id
				'Delete all by Section03Id
				Dim _sqlString As String = " DELETE FROM SECTION12Detail WHERE Section12ID=" & hidID.Value
				data.ExecuteStoreCommand(_sqlString)
				'' Insert Section03Detail
				If hidValue.Value <> "" Then
					Dim listData = hidValue.Value.Split("$")
					For index = 0 To listData.Count - 1
						Dim _p As New Section12Detail
						If listData(index) <> "" Then
							Dim _lst = listData(index).Split("#")
							If _lst.Length > 3 Then
								_p.Section12Id = hidID.Value
								_p.TenThanhPhan = _lst(0)
								_p.LoaiSinhVat = _lst(1)
								_p.ChukyAnhHuong = _lst(2)
								_p.KetQua = _lst(3)
								data.Section12Detail.AddObject(_p)
							End If
						End If
					Next
					data.SaveChanges()
				End If

				Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section11:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
				Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section13.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)

			Catch ex As Exception
				log4net.Config.XmlConfigurator.Configure()
				log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
				Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
			Finally
				GC.Collect()
			End Try
		End Using
	End Sub
#End Region
#Region "PRIAVTE SUB AND FUNCTION"
	Protected Sub Translate()
		lblSection12Title.Text = getTranslate("lblSection12Title", Session("LanguageId"), lblSection12Title.Text)
		lbSection12Title1.Text = getTranslate("lbSection12Title1", Session("LanguageId"), lbSection12Title1.Text)
		lblSection12TenTP.Text = getTranslate("lblSection12TenTP", Session("LanguageId"), lblSection12TenTP.Text)
		lbSection12LoaiSV.Text = getTranslate("lbSection12LoaiSV", Session("LanguageId"), lbSection12LoaiSV.Text)
		lblSection12CKAH.Text = getTranslate("lblSection12CKAH", Session("LanguageId"), lblSection12CKAH.Text)
		lblSection12KQ.Text = getTranslate("lblSection12KQ", Session("LanguageId"), lblSection12KQ.Text)
		lblSection11Thanhphan1.Text = getTranslate("lblSection11Thanhphan1", Session("LanguageId"), lblSection11Thanhphan1.Text)
		lblSection11Thanhphan2.Text = getTranslate("lblSection11Thanhphan2", Session("LanguageId"), lblSection11Thanhphan2.Text)
		lblSection11Thanhphan3.Text = getTranslate("lblSection11Thanhphan3", Session("LanguageId"), lblSection11Thanhphan3.Text)
		lblSection02Thanhphan4.Text = getTranslate("lblSection02Thanhphan4", Session("LanguageId"), lblSection02Thanhphan4.Text)

		lblSection12lbTacDongMT.Text = getTranslate("lblSection12lbTacDongMT", Session("LanguageId"), lblSection12lbTacDongMT.Text)
		hidDel.Value = getTranslate("strDelete", Session("LanguageId"), hidDel.Value)
	End Sub
	Sub CheckData()
		Using data As New VSEntities
			Dim LanguageId = Session("LanguageId").ToString()
			Dim p = (From q In data.MSDSSection12 Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value).FirstOrDefault()
            If Not p Is Nothing Then
				'txtS12110.Text = p.S12110
				'txtS12111.Text = p.S12111
				'txtS12112.Text = p.S12112
				'txtS12113.Text = p.S12113

				'txtS12120.Text = p.S12120
				'txtS12121.Text = p.S12121
				'txtS12122.Text = p.S12122
				'txtS12123.Text = p.S12123

				'txtS12130.Text = p.S12130
				'txtS12131.Text = p.S12131
				'txtS12132.Text = p.S12132
				'txtS12133.Text = p.S12133

				'txtS12140.Text = p.S12140
				'txtS12141.Text = p.S12141
				'txtS12142.Text = p.S12142
				'txtS12143.Text = p.S12143

                txtS1212.Text = p.S1221
                hidID.Value = p.Section12Id
            End If
		End Using
	End Sub
#End Region
End Class
